import {
	request
} from '@/util/request.js'
import {
	handleErrorCode
} from '@/util/common/message.js'

export async function onGotApiData(apiUrl, method, params, callback) {
		params.platform_no = 1
	await request({
		url: apiUrl,
		method: method,
		data: params,
		withCredentials: true,
	}).then(res => {
	const channel_no = uni.getStorageSync('channel_no')
		console.log("api请求成功",res)
		if (res.code && res.code == 200) {
			callback && callback(res.data);
		} else if(res.code && res.code == 401){
			uni.showToast({
				title: '信息发布成功',
				//将值设置为 success 或者直接不用写icon这个参数
				icon: 'none',
				//显示持续时间为 2秒
				duration: 2000
			}) 
			if(channel_no == 'YR_PPZ_V5_1001_ZFB'){
				uni.navigateBack({
					delta: 1
				});
				return
			}
			uni.navigateTo({
				url: '/pages/payvip_three/payvip_three'
			});
			
			return
		}else if(res.code && res.code == 402){
			console.log("结果返回402",res.data)
			if(channel_no == 'YR_PPZ_V5_1001_ZFB'){
				uni.showToast({
					title: '信息发布成功',
					//将值设置为 success 或者直接不用写icon这个参数
					icon: 'none',
					//显示持续时间为 2秒
					duration: 2000
				}) 
				uni.navigateBack({
					delta: 1
				});
				return
			}
			if(!res.data.sub.id){
				uni.navigateTo({
					url: '/pages/payvip_three/payvip_three?shop_id=' + res.data.shop_id + '&shop_type=' + res.data.shop_type + '&money=' + res.data.money,
				});
			}else{
					// 存储子渠道号和链接
				uni.setStorageSync('son_channel_no',res.data.sub.id);
				uni.setStorageSync('pages',res.data.sub.serverName);
				uni.navigateTo({
					url: res.data.sub.serverName + '?shop_id=' + res.data.shop_id + '&shop_type=' + res.data.shop_type + '&money=' + res.data.money,
				});
			}
			return
		}else{
			callback && callback(res);
		}
		
		
	}).catch(function(error) {
		console.log('捕捉到了错误',error)	
		// that.onMessageShow(error, 'error', 1500);
	});
}

export async function ongetphone(apiUrl, method, params, callback) {
		// params.platform_no = 6
	await request({
		url: apiUrl,
		method: method,
		data: params,
		withCredentials: true,
	}).then(res => {
	const channel_no = uni.getStorageSync('channel_no')
		console.log("手机号请求成功",res)
		callback && callback(res);
		
	}).catch(function(error) {
		console.log('捕捉到了错误',error)	
		// that.onMessageShow(error, 'error', 1500);
	});
}


//统计事件
export async function addClick(key, key2 = "") {
	let channel_no = uni.getStorageSync('channel_no')
	return new Promise((resolve, reject) => {
		uni.request({
			url: 'https://data-api.ksbmtb.com:8882/api/app/event/add', //仅为示例，并非真实接口地址。
			method: "POST",
			data: {
				event_name: key,
				project_name: 'shop-subletting',
				event_value: key2,
				// channel_no: "VXHead_CS"
				channel_no:channel_no,
			},
			header: {
				'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
			},
			success: async (res) => {
				resolve(res.result)
			}
		});
	});
}


export async function radioDF() {
	let res = await request({
		url: 'api/radio/radio',
		method: 'POST',
	})
	handleErrorCode(res.code, res.massage)
	return res
}
export async function createMerchant(data) {
	let res = await request({
		url: 'v4/merchant/createMerchant',
		method: 'POST',
		data
	})
	handleErrorCode(res.code, res.massage)
	return res
}
export async function getWxV2(data) {
	let res = await request({
		url: 'api/merchant_one_v2/getWxV2',
		method: 'POST',
		data
	})
	handleErrorCode(res.code, res.massage)
	return res
}
export async function feedback(data) {
	let res = await request({
		url: 'api/other/feedback',
		method: 'POST',
		data
	})
	handleErrorCode(res.code, res.massage)
	return res
}
export async function orderP(data) {
	let res = await request({
		url: 'v4/merchant/order',
		method: 'POST',
		data
	})
	handleErrorCode(res.code, res.massage)
	return res
}
export async function getMerchantByPhone(data) {
	let res = await request({
		url: 'v4/merchant/getMerchantByPhone',
		method: 'get',
		data
	})
	handleErrorCode(res.code, res.massage)
	return res
}
export async function updateSubMerchant(data) {
	let res = await request({
		url: 'v4/merchant/updateSubMerchant',
		method: 'POST',
		data
	})
	handleErrorCode(res.code, res.massage)
	return res
}
export async function getOpenIdDy(data) {
	let res = await request({
		url: '/util/getOpenIdDy',
		method: 'get',
		data
	})
	handleErrorCode(res.code, res.massage)
	return res
}
